<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Remark\DouYinRemarkRepository as model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;
use App\Repository\UploadRepository;
use App\Libs\PhpOffice\Excel;
use App\Repository\Shop\SuppilerRepository as supp;
use App\Repository\Order\OrderLogRepository as logModel;
use App\Repository\UpThree\MainOrderRepostition;

// 分销商
class CatchDouyinSignController extends BaseController
{
  public function __construct(model $model, supp $supp, logModel $logModel)
  {
    $this->repoist = $model;
    $this->disModel = $supp;
    $this->logModel = $logModel;
  }
  public function store(Request $request)
  {
    return $this->repoist->add($request);
  }
  public function index(Request $request)
  {
    $where = [];
    // if (($request->has('status'))) {
    //   $where['where']['status'] = $request->get('status');
    // }
    // if (!empty($request->get('user')->role_id) && $request->get('user')->role_id != 1 && empty($request->get('is_dist'))) {
    //   $where['where']['shop_id'] = $request->get('user')->shop_id;
    // }
    // if (!empty($request->get('shop_id'))) {
    //   $where['like']['shop_id_str'] = $request->get('user')->shop_id . '.' . $request->get('shop_id');
    // }
    // if (!empty($request->get('dist_orderno'))) {
    //   $where['where']['dist_orderno'] = trim($request->get('dist_orderno'), ' ');
    // }
    // if (!empty($request->get('identity'))) {
    //   $where['where']['identity'] = trim($request->get('identity'), ' ');
    // }
    // //店铺名称
    // if (!empty($request->get('dist'))) {
    //   $where['where']['dist_id'] = trim($request->get('dist'), ' ');
    // }
    // if (!empty($request->get('iccid'))) {
    //   $where['where']['iccid'] = $request->get('iccid');
    //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    // }
    // if (!empty($request->get('express_number'))) {
    //   $where['where']['express_number'] = $request->get('express_number');
    //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    // }
    // if (!empty($request->get('contact'))) {
    //   $where['where']['contact'] = $request->get('contact');
    //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    // }
    // if (!empty($request->get('name'))) {
    //   $where['where']['name'] = $request->get('name');
    //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    // }
    //时间验证 
    $data =  $this->repoist->index($request);
    return $data;
  }

  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = [
      'id' => $request->get('id'),
      // 'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->edit($where, $request);
  }
  public function  show(Request $request, $id)
  {
    $where = [
      'order_num' => $id
    ];
    //查询一条数据
    $field = ['*'];
    $return = $this->repoist->getOne($where, $field);
    return $this->returnMsg(200, '查询成功', $return);
  }
  public function  showOne($id)
  {
    $where = ['order_num' => $id];
    //查询一条数据
    $return = $this->repoist->getOne($where, ['*']);
    return $this->returnMsg(200, '查询成功', $return);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['name' => $request->get('name')];
    }

    $return = $this->repoist->lists($where);
    return $return;
  }

  public function destroy(Request $request, $id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->del($where);
  }
  // 导入功能
  public function import(Request $request)
  {
    $model = new UploadRepository();
    $res = $model->upload($request, 'file');
    if (empty($res['file_src'])) {
      return [
        'code' => 100,
        'msg' => '上传失败',
        'fileName' => ''
      ];
    }
    $request['file_name'] = $res['file_name'];
    $request['upload_rout'] = $res['file_src'];

    return $this->repoist->add($request);
  }
  public function getDouYInOrderStatus()
  {
    return $this->repoist->getOrderStatus();
  }
  //导出
  public function getOrderExcel(Request $request)
  {
    // pageNum  pageSize
    $request->offsetSet('pageSize', 99999);
    $request->offsetSet('pageNum', 1);
    $resData = $this->index($request);
    if (empty($resData['data'])) {
      return $this->returnMsg(100, '下载的数据不能为空');
    }
    $model = new Excel();
    $title = [
      '订单编号',
      '上游编号',
      '下游编号',
      '来源',
      '分销商',
      '用户姓名',
      '用户联系电话',
      '证件信息',
      '省',
      '市',
      '区',
      '详细地址',
      '订单状态'
    ];
    $excelData = [];
    foreach ($resData['data'] as $key => $v) {
      $excelData[] = [
        $v['orderno'],
        $v['sup_orderno'],
        $v['dist_orderno'],
        $v['source'],
        $v['shop_str'],
        $v['pay_name'],
        $v['pay_phone'],
        $v['pay_identity'] . ' ',
        $v['post_province'],
        $v['post_city'],
        $v['post_area'],
        $v['post_address'],
        $v['status_desc'],
      ];
    }
    $fielname = '/attach/excel/' . date('YmdHis') . '.xlsx';
    $model->exportFile($excelData, $title, $fielname);
    return $this->returnMsg(200, '导出成功', ['fiel' => $fielname]);
  }
  public function catchDouyinThree(Request $request)
  {
    if (!$request->has('id')) {
      return $this->returnMsg(1, '参数不能为空');
    }
    $where = ['dist_orderno' => $request->get('id')];
    $oneData = $this->repoist->getOne($where);
    if (!$oneData) {
      return  $this->returnMsg(2, '未查询到订单');
    }
    // 将用户提交到第三方
    $orderData = [
      'source' => 1,  // 数据来源
      // 'goods_code' => $oneData['goods_code'],
      // 'name' => ($resDouYinDetails['code'] == 200) ? $resDouYinDetail['user_name'] : '',
      // 'identity' => ($resDouYinDetails['code'] == 200) ? $resDouYinDetail['card_num'] : '',
      // 'contact' => ($resDouYinDetails['code'] == 200) ? $resDouYinDetail['user_phone'] : '',
      // 'province' => $resDouYinDetail['province'],
      // 'city' => $resDouYinDetail['city'],
      // 'area' => $resDouYinDetail['area'],
      // 'address' => (!empty($resDouYinDetail['street']) ? $resDouYinDetail['street'] : '') . (($resDouYinDetails['code'] == 200) ? $resDouYinDetail['addr'] : ''),
      // 'shop_id' => $resDouYin->shop_id,
      // 'dist_orderno' => $resDouYinDetail['order_num'],
      // 'dist_id' => $resDouYin->id,
      // 'dist_name' => !empty($resDouYin->remark) ? $resDouYin->remark : '',
      // 'author_id' => $resDouYinDetail['author_id'],
      // 'author_name' => $resDouYinDetail['author_name'],
      // 'remark' => $resDouYinDetail['c_biz_desc'],
    ];
    $orderData = json_decode(json_encode($oneData), true);
    $orderData['source'] = 1;
    if (!empty($orderData['id'])) {
      unset($orderData['id']);
    }
    // var_dump($orderData);
    // die();
    $model = new MainOrderRepostition();
    $res = $model->orderSubmit($orderData);
    $update['three_msg'] = !empty($res['msg']) ? $res['msg'] : '提交失败';
    if (!empty($res['code']) && $res['code'] == 200) {
      $update['status'] = 0;
    }
    $this->repoist->edit($where, $update);
    return $res;
  }
}
